using System;
using System.Collections.Generic;
using System.Text;
using ECommerce.Library.Web;
using ECommerce.Library.DataStructure;
using System.Web;

namespace AutoGear.BLL
{
    public class AutoGearSessionManager
    {
        public static void Clear()
        {
            throw new Exception("method not implemented");
        }


        public static void AddTemporaryStock(ProductStock stock)
        {
            List<ProductStock> stocks = (List<ProductStock>)System.Web.HttpContext.Current.Session[STOCKS];
            if (stocks != null)
                stocks.Add(stock);
            else
            {
                stocks = new List<ProductStock>();
                stocks.Add(stock);
            }
            System.Web.HttpContext.Current.Session[STOCKS] = stocks;
        }

        public static void RemoveTemporaryStock(List<int> positions)
        {
            List<ProductStock> stocks = (List<ProductStock>)System.Web.HttpContext.Current.Session[STOCKS];
            List<ProductStock> itemsToDelete = new List<ProductStock>();
            if (stocks != null)
            {
                foreach (int itemPosition in positions)
                {
                    itemsToDelete.Add(stocks[itemPosition]);
                }
            }

            foreach (ProductStock item in itemsToDelete)
            {
                stocks.Remove(item);
            }

            System.Web.HttpContext.Current.Session[STOCKS] = stocks;
        }

        public static void ClearTemporaryStock()
        {
            System.Web.HttpContext.Current.Session[STOCKS] = null;
        }

        public static List<ProductStock> TemporaryStocks
        {
            get { return (List<ProductStock>)System.Web.HttpContext.Current.Session[STOCKS]; }
            set { System.Web.HttpContext.Current.Session[STOCKS] = value; }
        }

        public static string SearchKeyword
        {
            get
            {
                string data = Convert.ToString(System.Web.HttpContext.Current.Session[SEARCH_KEYWORDS]);
                data=HttpContext.Current.Server.HtmlDecode(data);
                System.Web.HttpContext.Current.Session[SEARCH_KEYWORDS] = string.Empty;
                return data;
            }
            set { System.Web.HttpContext.Current.Session[SEARCH_KEYWORDS] = value; }
        }

        public static string SearchItemNumber
        {
            get
            {
                string data = Convert.ToString(System.Web.HttpContext.Current.Session[SEARCH_ITEM_NUMBER]);
                System.Web.HttpContext.Current.Session[SEARCH_ITEM_NUMBER] = string.Empty;
                return data;
            }
            set { System.Web.HttpContext.Current.Session[SEARCH_ITEM_NUMBER] = value; }
        }

        // Property used for Shopping cart
        public static ShoppingCart ShoppingCart
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[SHOPPING_CART] != null)
                    return (ShoppingCart)System.Web.HttpContext.Current.Session[SHOPPING_CART];

                return new ShoppingCart();
            }
            set { System.Web.HttpContext.Current.Session[SHOPPING_CART] = value; }
        }

        public static ReadWriteCustomCollection<Favorites> FavoritesList
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[FAVORITES_LIST] != null)
                    return (ReadWriteCustomCollection<Favorites>)System.Web.HttpContext.Current.Session[FAVORITES_LIST];

                return new ReadWriteCustomCollection<Favorites>();
            }
            set { System.Web.HttpContext.Current.Session[FAVORITES_LIST] = value; }
        }

        public static ProductShipping ProductShipping
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[PRODUCT_SHIPPING] != null)
                    return (ProductShipping)System.Web.HttpContext.Current.Session[PRODUCT_SHIPPING];

                return new ProductShipping();
            }
            set { System.Web.HttpContext.Current.Session[PRODUCT_SHIPPING] = value; }
        }

        public static StateTax StateTax
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[STATE_TAX] != null)
                    return (StateTax)System.Web.HttpContext.Current.Session[STATE_TAX];
                return new StateTax();
            }
            set { System.Web.HttpContext.Current.Session[STATE_TAX] = value; }
        }

        public static CreditCard Card
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[CREDIT_CARD] != null)
                    return (CreditCard)System.Web.HttpContext.Current.Session[CREDIT_CARD];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[CREDIT_CARD] = value; }
        }

        public static Address BillingAddress
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[BILLING_ADDRESS] != null)
                    return (Address)System.Web.HttpContext.Current.Session[BILLING_ADDRESS];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[BILLING_ADDRESS] = value; }
        }

        public static Address ShippingAddress
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[SHIPPING_ADDRESS] != null)
                    return (Address)System.Web.HttpContext.Current.Session[SHIPPING_ADDRESS];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[SHIPPING_ADDRESS] = value; }
        }

        public static string OrderNumber
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[ORDER_NUMBER] != null)
                    return (string)System.Web.HttpContext.Current.Session[ORDER_NUMBER];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[ORDER_NUMBER] = value; }
        }

        public static CheckoutSteps CheckoutSteps
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[CHECKOUT_STEPS] != null)
                    return (CheckoutSteps)System.Web.HttpContext.Current.Session[CHECKOUT_STEPS];
                return CheckoutSteps.OtherPages;
            }
            set { System.Web.HttpContext.Current.Session[CHECKOUT_STEPS] = value; }
        }

        public static Coupon Coupon
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[COUPON] != null)
                    return (Coupon)System.Web.HttpContext.Current.Session[COUPON];
                return null;
            }
            set { System.Web.HttpContext.Current.Session[COUPON] = value; }
        }

        // Property used for Shopping cart
        public static List<Product> RecentlyViewedProducts
        {
            get
            {
                if (System.Web.HttpContext.Current.Session[RECENTLY_VIEWED_PRODUCTS] != null)
                    return (List<Product>)System.Web.HttpContext.Current.Session[RECENTLY_VIEWED_PRODUCTS];

                return new List<Product>();
            }
            set 
            {
                System.Web.HttpContext.Current.Session[RECENTLY_VIEWED_PRODUCTS] = value; 
            }
        }

        private const string STOCKS = "Stocks";
        private const string SHOPPING_CART = "Shopping_Cart";
        private const string SEARCH_KEYWORDS = "Search_Keywords";
        private const string SEARCH_ITEM_NUMBER = "Search_Item_Number";
        private const string FAVORITES_LIST = "Favorites_List";
        private const string PRODUCT_SHIPPING = "Product_Shipping";
        private const string STATE_TAX = "State_Tax";
        private const string CREDIT_CARD = "Credit_Card";
        private const string BILLING_ADDRESS = "Billing_Address";
        private const string SHIPPING_ADDRESS = "Shipping_Address";
        private const string ORDER_NUMBER = "Order_Number";
        private const string CHECKOUT_STEPS = "CheckOut_Steps";
        private const string COUPON = "Coupon";
        private const string RECENTLY_VIEWED_PRODUCTS = "RecentlyViewedProducts";
    }
}

